package com.znsd.stu.controllor;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.znsd.entitys.Cwj_ClassInfo;
import com.znsd.entitys.Users;
import com.znsd.stu.service.Cwj_UserService;

@Controller
public class Cwj_UserControllor {
	@Autowired
	private Cwj_UserService users;
	
	@RequestMapping(value = "goupdateteacher", method = RequestMethod.GET)
	public String goupdateteacher(Integer userId,HttpServletRequest  request) {
		HttpSession session=request.getSession();
		session.setAttribute("userId",userId);
		return "admin/teacheredit";
	}
	@RequestMapping(value = "goteacher", method = RequestMethod.GET)
	public String goteacher() {
		return "admin/teachers";
	}
	@RequestMapping(value = "goUpdateClassStu", method = RequestMethod.GET)
	public String goUpdateClassStu(Integer stuId,HttpServletRequest  request) {
		HttpSession session=request.getSession();
		session.setAttribute("stuId",stuId);
		System.out.println(session.getAttribute("stuId"));
		return "admin/updatestupost";
	}
	@ResponseBody
	@RequestMapping("getStudentsByClass")
	public Map<String,Object> getStudentsByClass(HttpServletRequest  request) {
		HttpSession session=request.getSession();
		Integer stuId=(Integer) session.getAttribute("stuId");
		System.out.println(stuId);
		Users Students=users.getByTeaher(stuId);
		System.out.println(Students);
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("students",Students);
		return map;
	}
	
	@RequestMapping(value = "updateStuByClassPost", method = RequestMethod.POST)
	public String updateStuByClassPost(Integer hiddenId,Integer class1) {
		users.updateStuByClassPost(class1, hiddenId);
		return "admin/stuclass";
	}
	@RequestMapping(value = "teacherPost", method = RequestMethod.POST)
	public String teacherPost(Integer user_id,String user_name,
			Integer user_age,String user_phone,
			String user_mail,String user_idCard,
			String user_birthday,String user_native,
			String user_home,String user_education,
			String user_nation,String graduate_time,
			Integer user_married,Integer user_sex,Integer user_state) throws ParseException {
		Date userdate1=new SimpleDateFormat("yyyy-MM-dd").parse(user_birthday);
		Date userdate2=new SimpleDateFormat("yyyy-MM-dd").parse(graduate_time);
		users.updateteacher(user_id, user_name, user_age, user_phone, user_mail, user_idCard,userdate1, user_native, user_home, user_education, user_nation,userdate2, user_married, user_sex,user_state);
		return "admin/teachers";
	}
	
	@ResponseBody
	@RequestMapping("getTeacherById")
	public Map<String,Object> getTeacherById(HttpServletRequest  request) {
		HttpSession session=request.getSession();
		Integer userId=(Integer) session.getAttribute("userId");
		Users t_user=users.getByTeaher(userId);
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("teacher",t_user);
		return map;
	}
	@ResponseBody
	@RequestMapping("ListTeachers")
	public Map<String,Object> ListTeachers(HttpServletRequest  request) {
		List<Users> Myuser=users.gettecherAll();
		Map<String,Object> map=new HashMap<String,Object>();
		/* Integer count=users.getCountTeacher(); */
		map.put("rows",Myuser);
		map.put("total",Myuser.size());
		return map;
	}
	
	@ResponseBody
	@RequestMapping("ListStudents")
	public Map<String,Object> ListStudents(HttpServletRequest  request) {
		HttpSession session=request.getSession();
		Integer classId=(Integer) session.getAttribute("classId");
		List<Users> Myuser=users.getStuByClass(classId);
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("rows",Myuser);
		map.put("total",Myuser.size());
		return map;
	}
	@ResponseBody
	@RequestMapping("delTeacher")
	public Map<String,Object> delTeacher(Integer id,HttpServletRequest  request) {
		users.delete(id);
		Map<String,Object> map=new HashMap<String,Object>();
		return map;
	}
}
